<template>
  <div class="app-container">
      <div class="filter-container" style="margin-bottom:10px">
          <el-input
                  v-model="queryParams.machineCode"
                  placeholder="请输入门禁编码"
                  style="width: 200px;"
                  class="filter-item"
          />
          <el-input
                  v-model="queryParams.machineIp"
                  placeholder="请输入门禁IP"
                  style="width: 200px;"
                  class="filter-item"
          />

          <el-input
                  v-model="queryParams.machineMac"
                  placeholder="请输入门禁Mac"
                  style="width: 200px;"
                  class="filter-item"
          />

          <el-button
                  v-waves
                  class="filter-item"
                  type="primary"
                  icon="el-icon-search"
                  @click="queryMachine"
          >查询门禁</el-button>
          <el-button
                  class="filter-item"
                  style="margin-left: 10px;"
                  type="primary"
                  icon="el-icon-edit"
                  @click="addAccessControl"
          >添加门禁</el-button>
      </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['access:machine:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['access:machine:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['access:machine:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['access:machine:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

      <el-table
              v-loading="loading"
              :data="machineList"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row
      >
          <el-table-column align="center" label="编号" width="60">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
          </el-table-column>
          <el-table-column align="center" label="名称">
              <template slot-scope="scope">{{ scope.row.machineName }}</template>
          </el-table-column>
          <el-table-column align="center" label="门禁编码">
              <template slot-scope="scope">{{ scope.row.machineCode }}</template>
          </el-table-column>
          <el-table-column label="门禁IP" align="center">
              <template slot-scope="scope">
                  <span>{{ scope.row.machineIp }}</span>
              </template>
          </el-table-column>
          <el-table-column label="版本号" align="center">
              <template slot-scope="scope">{{ scope.row.machineVersion }}</template>
          </el-table-column>
          <el-table-column class-name="status-col" label="mac地址" align="center">
              <template slot-scope="scope">{{ scope.row.machineMac }}</template>
          </el-table-column>
          <el-table-column class-name="status-col" label="厂商" align="center">
              <template slot-scope="scope">{{ scope.row.oem }}</template>
          </el-table-column>
          <el-table-column class-name="status-col" label="操作" width="300" align="center">
              <template slot-scope="{row,$index}">
                  <el-row>
                      <el-button size="mini"  type="primary" @click="openDoor(row,$index)">开门</el-button>
                      <el-button size="mini"  type="primary" @click="viewFace(row,$index)">人脸</el-button>
                      <el-button size="mini"  type="warning" @click="restartAccessControl(row,$index)">重启</el-button>
                      <el-button size="mini"  type="danger" @click="deleteAccessControl(row,$index)">删除</el-button>
                  </el-row>
              </template>
          </el-table-column>
      </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门禁设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编码" prop="machineCode">
          <el-input v-model="form.machineCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备版本" prop="machineVersion">
          <el-input v-model="form.machineVersion" placeholder="请输入设备版本" />
        </el-form-item>
        <el-form-item label="设备名称" prop="machineName">
          <el-input v-model="form.machineName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型 门禁9999 详情查看t_dict 表" prop="machineTypeCd">
          <el-input v-model="form.machineTypeCd" placeholder="请输入设备类型 门禁9999 详情查看t_dict 表" />
        </el-form-item>
        <el-form-item label="设备IP" prop="machineIp">
          <el-input v-model="form.machineIp" placeholder="请输入设备IP" />
        </el-form-item>
        <el-form-item label="设备mac" prop="machineMac">
          <el-input v-model="form.machineMac" placeholder="请输入设备mac" />
        </el-form-item>
        <el-form-item label="数据状态，详细参考c_status表，S 保存，0, 在用 1失效" prop="statusCd">
          <el-input v-model="form.statusCd" placeholder="请输入数据状态，详细参考c_status表，S 保存，0, 在用 1失效" />
        </el-form-item>
        <el-form-item label="生产厂商" prop="oem">
          <el-input v-model="form.oem" placeholder="请输入生产厂商" />
        </el-form-item>
        <el-form-item label="外部设备ID" prop="extMachineId">
          <el-input v-model="form.extMachineId" placeholder="请输入外部设备ID" />
        </el-form-item>
        <el-form-item label="小区ID" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID" />
        </el-form-item>
        <el-form-item label="协议ID" prop="hmId">
          <el-input v-model="form.hmId" placeholder="请输入协议ID" />
        </el-form-item>
        <el-form-item label="设备心跳时间" prop="heartbeatTime">
          <el-date-picker clearable size="small"
            v-model="form.heartbeatTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择设备心跳时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="位置类型" prop="locationType">
          <el-select v-model="form.locationType" placeholder="请选择位置类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="位置ID" prop="locationObjId">
          <el-input v-model="form.locationObjId" placeholder="请输入位置ID" />
        </el-form-item>
        <el-form-item label="设备方向进出，3306 进 3307 出" prop="direction">
          <el-input v-model="form.direction" placeholder="请输入设备方向进出，3306 进 3307 出" />
        </el-form-item>
        <el-form-item label="第三方平台ID" prop="thirdMachineId">
          <el-input v-model="form.thirdMachineId" placeholder="请输入第三方平台ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMachine, getMachine, delMachine, addMachine, updateMachine, exportMachine } from "@/api/access/machine";

export default {
  name: "Machine",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门禁设备表格数据
      machineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        machineCode: null,
        machineVersion: null,
        machineName: null,
        machineTypeCd: null,
        machineIp: null,
        machineMac: null,
        statusCd: null,
        oem: null,
        extMachineId: null,
        communityId: null,
        hmId: null,
        heartbeatTime: null,
        locationType: null,
        locationObjId: null,
        direction: null,
        thirdMachineId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        machineCode: [
          { required: true, message: "设备编码不能为空", trigger: "blur" }
        ],
        machineVersion: [
          { required: true, message: "设备版本不能为空", trigger: "blur" }
        ],
        machineName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        machineTypeCd: [
          { required: true, message: "设备类型 门禁9999 详情查看t_dict 表不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        statusCd: [
          { required: true, message: "数据状态，详细参考c_status表，S 保存，0, 在用 1失效不能为空", trigger: "blur" }
        ],
        heartbeatTime: [
          { required: true, message: "设备心跳时间不能为空", trigger: "blur" }
        ],
        locationType: [
          { required: true, message: "位置类型不能为空", trigger: "change" }
        ],
        direction: [
          { required: true, message: "设备方向进出，3306 进 3307 出不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门禁设备列表 */
    getList() {
      this.loading = true;
      listMachine(this.queryParams).then(response => {
        this.machineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        machineId: null,
        machineCode: null,
        machineVersion: null,
        machineName: null,
        machineTypeCd: null,
        machineIp: null,
        machineMac: null,
        createTime: null,
        statusCd: null,
        oem: null,
        extMachineId: null,
        communityId: null,
        hmId: null,
        heartbeatTime: null,
        locationType: null,
        locationObjId: null,
        direction: null,
        thirdMachineId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.machineId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const machineId = row.machineId || this.ids
      getMachine(machineId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改门禁设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.machineId != null) {
            updateMachine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMachine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const machineIds = row.machineId || this.ids;
      this.$confirm('是否确认删除门禁设备编号为"' + machineIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMachine(machineIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有门禁设备数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMachine(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
